<?php

namespace app\web\jwt;

use Firebase\JWT\JWT;
use http\Exception\UnexpectedValueException;
use think\Exception;
use think\facade\Env;

class JwtsMac
{
    /**
     * 解密token
     * @param $token
     */
    public static function verifyToken($token){
        try {
            JWT::$leeway = 60;//当前时间减去60，把时间留点余地
            $decoded = JWT::decode($token, Env::get('MAC_JWT_KEY'), ['HS256']); //HS256方式，这里要和签发的时候对应
//            $mac_id = redis()->get(self::$config['name'].':'.$decoded->data->mac_id);

            if(!isset($decoded->data->mac_id)){
                throw new \Firebase\JWT\ExpiredException('设备授权码非法！');
            }
        }catch (Exception $exception){
            throw new \Firebase\JWT\ExpiredException($exception->getMessage());
        }
        return (array)$decoded;
    }
}